library(ggthemr)
ggthemr('flat', spacing = 0.5, type = 'inner',layout="scientific")
anes2012$oldnew <- relevel(as.factor(anes2012$oldnew),ref="Old")
anes2012$oldnew2 <-  relevel(as.factor(anes2012$oldnew2),ref="Old")
a <- (lm(pk~efficacy1pre*agreeableness*oldnew,anes2012))
summary(lm(pk~efficacy1pre*oldnew,subset(anes2012,agreeableness<.5)))


b <- (lm(pk~efficacy2pre*agreeableness*oldnew,anes2012))
c <- (lm(pk~efficacy3pre*agreeableness*oldnew,anes2012))
d <- (lm(pk~efficacy4pre*agreeableness*oldnew,anes2012))
e <- (lm(pk~pre_external*agreeableness*oldnew,anes2012))
f <- (lm(pk~pre_internal*agreeableness*oldnew,anes2012))

g <- (lm(pk~efficacy1po*agreeableness*oldnew2,anes2012))
h <- (lm(pk~efficacy2po*agreeableness*oldnew2,anes2012))
i <- (lm(pk~efficacy3po*agreeableness*oldnew2,anes2012))
j <- (lm(pk~efficacy4po*agreeableness*oldnew2,anes2012))
k <- (lm(pk~po_external*agreeableness*oldnew2,anes2012))
l <- (lm(pk~po_internal*agreeableness*oldnew2,anes2012))

names(a$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(b$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(c$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(d$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(e$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(f$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(g$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(h$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(i$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(j$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(k$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(l$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )

library(stargazer)
stargazer(a,b,c,d,e,f,g,h,i,j,k,l,omit.stat =c("f","ser"),float.env = "sidewaystable",dep.var.caption = "Efficacy Measure",column.labels =   c("1","2","3","4","External","Internal","1","2","3","4","External","Internal"),model.numbers = F,no.space = T,dep.var.labels.include = F,title="Predicting political knowledge from three-way interactions between question type, efficacy, and agreeableness",label = "tab:agreeableness_pk",out = "Graphs/threewayagreeableness_pk.tex",initial.zero = T,digits = 2,font.size = "scriptsize",intercept.top = T,intercept.bottom=F)

threeway1 <- data.frame(do.call("rbind",lapply(list(a,b,c,d,e,f,g,h,i,j,k,l),function(x)summary(x)$coefficients[8,1:2])))
threeway1$question <- c("Efficacy 1","Efficacy 2","Efficacy 3","Efficacy 4","External\n Index","Internal\n Index","Efficacy 1","Efficacy 2","Efficacy 3","Efficacy 4","External\n Index","Internal\n Index")
threeway1$wave <- c(rep("Wave 1",6),rep("Wave 2",6))
names(threeway1)[2] <- "se"
panela <- ggplot(threeway1,aes(y=Estimate,x=question))+geom_errorbar(aes(ymax=Estimate+1.64*se,ymin=Estimate-1.64*se),height=0)+geom_point(size=2,colour="black")+facet_grid(~wave)+xlab("Question")+ylab("b coefficient")+ggtitle("Panel A: DV is R's political knowledge")+geom_hline(y=0)




########################################
########################################
a <- (lm(pctchance~efficacy1pre*agreeableness*oldnew,anes2012))
b <- (lm(pctchance~efficacy2pre*agreeableness*oldnew,anes2012))
c <- (lm(pctchance~efficacy3pre*agreeableness*oldnew,anes2012))
d <- (lm(pctchance~efficacy4pre*agreeableness*oldnew,anes2012))
e <- (lm(pctchance~pre_external*agreeableness*oldnew,anes2012))
f <- (lm(pctchance~pre_internal*agreeableness*oldnew,anes2012))

g <- (lm(pctchance~efficacy1po*agreeableness*oldnew2,anes2012))
h <- (lm(pctchance~efficacy2po*agreeableness*oldnew2,anes2012))
i <- (lm(pctchance~efficacy3po*agreeableness*oldnew2,anes2012))
j <- (lm(pctchance~efficacy4po*agreeableness*oldnew2,anes2012))
k <- (lm(pctchance~po_external*agreeableness*oldnew2,anes2012))
l <- (lm(pctchance~po_internal*agreeableness*oldnew2,anes2012))
summary(lm(polactivity~efficacy3po*oldnew2,subset(anes2012,as.numeric(agreeableness)==2)))

names(a$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(b$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(c$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(d$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(e$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(f$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(g$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(h$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(i$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(j$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(k$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(l$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )

library(stargazer)
stargazer(a,b,c,d,e,f,g,h,i,j,k,l,omit.stat =c("f","ser"),float.env = "sidewaystable",dep.var.caption = "Efficacy Measure",column.labels =   c("1","2","3","4","External","Internal","1","2","3","4","External","Internal"),model.numbers = F,no.space = T,dep.var.labels.include = F,title="Predicting percent chance of voting from three-way interactions between question type, efficacy, and agreeableness",label = "tab:agreeableness_pctchace",out = "Graphs/threewayvotingagreeableness.tex",initial.zero = T,digits = 2,font.size = "scriptsize",intercept.top = T,intercept.bottom=F)


threeway1 <- data.frame(do.call("rbind",lapply(list(a,b,c,d,e,f,g,h,i,j,k,l),function(x)summary(x)$coefficients[8,1:2])))
threeway1$question <- c("Efficacy 1","Efficacy 2","Efficacy 3","Efficacy 4","External\n Index","Internal\n Index","Efficacy 1","Efficacy 2","Efficacy 3","Efficacy 4","External\n Index","Internal\n Index")
threeway1$wave <- c(rep("Wave 1",6),rep("Wave 2",6))
names(threeway1)[2] <- "se"
panelb <- ggplot(threeway1,aes(y=Estimate,x=question))+geom_errorbar(aes(ymax=Estimate+1.64*se,ymin=Estimate-1.64*se),height=0)+geom_point(size=2,colour="black")+facet_grid(~wave)+xlab("Question")+ylab("b coefficient")+ggtitle("Panel B: DV is R's Percent Chance of Voting")+geom_hline(y=0)

########################################
a <- (lm(polactivity~efficacy1pre*agreeableness*oldnew,anes2012))
b <- (lm(polactivity~efficacy2pre*agreeableness*oldnew,anes2012))
c <- (lm(polactivity~efficacy3pre*agreeableness*oldnew,anes2012))
d <- (lm(polactivity~efficacy4pre*agreeableness*oldnew,anes2012))
e <- (lm(polactivity~pre_external*agreeableness*oldnew,anes2012))
f <- (lm(polactivity~pre_internal*agreeableness*oldnew,anes2012))

g <- (lm(polactivity~efficacy1po*agreeableness*oldnew2,anes2012))
h <- (lm(polactivity~efficacy2po*agreeableness*oldnew2,anes2012))
i <- (lm(polactivity~efficacy3po*agreeableness*oldnew2,anes2012))
j <- (lm(polactivity~efficacy4po*agreeableness*oldnew2,anes2012))
k <- (lm(polactivity~po_external*agreeableness*oldnew2,anes2012))
l <- (lm(polactivity~po_internal*agreeableness*oldnew2,anes2012))



names(a$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(b$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(c$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(d$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(e$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(f$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(g$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(h$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(i$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(j$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(k$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )
names(l$coefficients) <- c("Intercept","Efficacy","Agreeableness","Form","Effic. x Agreeableness","Effic. x Form","agreeableness x Form","Effic. x Agreeableness x Form" )

library(stargazer)
stargazer(a,b,c,d,e,f,g,h,i,j,k,l,omit.stat =c("f","ser"),float.env = "sidewaystable",dep.var.caption = "Efficacy Measure",column.labels =   c("1","2","3","4","External","Internal","1","2","3","4","External","Internal"),model.numbers = F,no.space = T,dep.var.labels.include = F,title="Predicting political activism from three-way interactions between question type, efficacy, and agreeableness",label = "tab:agreeableness_activism",out = "Graphs/threewayagreeableness_activism.tex",initial.zero = T,digits = 2,font.size = "scriptsize",intercept.top = T,intercept.bottom=F)

threeway1 <- data.frame(do.call("rbind",lapply(list(a,b,c,d,e,f,g,h,i,j,k,l),function(x)summary(x)$coefficients[8,1:2])))
threeway1$question <- c("Efficacy 1","Efficacy 2","Efficacy 3","Efficacy 4","External\n Index","Internal\n Index","Efficacy 1","Efficacy 2","Efficacy 3","Efficacy 4","External\n Index","Internal\n Index")
threeway1$wave <- c(rep("Wave 1",6),rep("Wave 2",6))
names(threeway1)[2] <- "se"
panelc <- ggplot(threeway1,aes(y=Estimate,x=question))+geom_errorbar(aes(ymax=Estimate+1.64*se,ymin=Estimate-1.64*se),height=0)+geom_point(size=2,colour="black")+facet_grid(~wave)+xlab("Question")+ylab("b coefficient")+ggtitle("Panel B: DV is R's political activity")+geom_hline(y=0)


pdf("threewayplot_agreeableness.pdf",width=12,height=12)
multiplot(panela,panelb,panelc)
dev.off()

